package com.example.demo.controller;

import com.example.demo.common.AppVariable;
import com.example.demo.common.ResultAjax;
import com.example.demo.common.SessionUtils;
import com.example.demo.model.Articleinfo;
import com.example.demo.model.Userinfo;
import com.example.demo.model.vo.UserinfoVO;
import com.example.demo.service.ArticleService;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.stream.Stream;

@RestController
@RequestMapping("/art")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    public static final int LENGTH=80;

    @Autowired
    private UserService userService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @RequestMapping("/mylist")
    public ResultAjax myList(HttpServletRequest request){
        Userinfo userinfo=SessionUtils.getUser(request);
        System.out.println(userinfo.toString());
        if(userinfo==null){
            return ResultAjax.fail(-1,"请先登录");
        }
        List<Articleinfo> list=articleService.getListByUid(userinfo.getId());
        if(list!=null&&list.size()>0){
            list.stream().parallel().forEach((art)->{
                if(art.getContent().length()>LENGTH){
                    art.setContent(art.getContent().substring(0,LENGTH));
                }
            });
        }
        return ResultAjax.succ(list);
    }

    @RequestMapping("/del")
    public ResultAjax del(Integer id,HttpServletRequest request){
        if(id==null||id<=0){
            return ResultAjax.fail(-1,"参数有误");
        }
        Userinfo userinfo=SessionUtils.getUser(request);
        if(userinfo==null){
            return ResultAjax.fail(-2,"请先登录");
        }
        int result=articleService.del(id,userinfo.getId());
        return ResultAjax.succ(result);
    }

    @RequestMapping("/add")
    public ResultAjax add(Articleinfo articleinfo,HttpServletRequest request){
        if(articleinfo==null||!StringUtils.hasLength(articleinfo.getTitle())||!StringUtils.hasLength(articleinfo.getContent())){
            return ResultAjax.fail(-1,"参数错误");
        }
        Userinfo userinfo=SessionUtils.getUser(request);
        if(userinfo==null){
            return ResultAjax.fail(-2,"用户未登录");
        }
        articleinfo.setUid(userinfo.getId());
        int result=articleService.insertByUid(articleinfo);
        return ResultAjax.succ(result);
    }

    @RequestMapping("/init_update")
    public ResultAjax initUpdate(Integer id,HttpServletRequest request){
        if(id==null||id<=0){
            return ResultAjax.fail(-1,"参数错误");
        }
        Userinfo userinfo=SessionUtils.getUser(request);
        if(userinfo==null){
            return ResultAjax.fail(-2,"用户未登录");
        }
        Articleinfo articleinfo=articleService.getArticleByIdAndUid(id,userinfo.getId());
        return ResultAjax.succ(articleinfo);
    }

    @RequestMapping("/update")
    public ResultAjax update(Articleinfo articleinfo,HttpServletRequest request){
        if(articleinfo==null||
                !StringUtils.hasLength(articleinfo.getTitle())||
                !StringUtils.hasLength(articleinfo.getContent())||
                articleinfo.getId()==0){
            return ResultAjax.fail(-1,"参数错误");
        }
        Userinfo userinfo=SessionUtils.getUser(request);
        if(userinfo==null){
            return ResultAjax.fail(-2,"用户未登录");
        }
        articleinfo.setUid(userinfo.getId());
        int result=articleService.updateByIdAndUid(articleinfo);
        return ResultAjax.succ(result);
    }

    @RequestMapping("/detail")
    public ResultAjax detail(Integer id) throws ExecutionException, InterruptedException {
        if(id==null||id<=0){
            return ResultAjax.fail(-1,"参数错误");
        }

        Articleinfo articleinfo=articleService.getDetail(id);
        if(articleinfo==null||articleinfo.getId()<=0){
            return ResultAjax.fail(-1,"参数错误");
        }

        FutureTask<UserinfoVO> userTask=new FutureTask(()->{
            return userService.getUserById(articleinfo.getUid());
        });
        taskExecutor.submit(userTask);

        FutureTask<Integer> artCountTask=new FutureTask(()->{
            return articleService.getartCountByUid(articleinfo.getUid());
        });
        taskExecutor.submit(artCountTask);

        UserinfoVO userinfoVO=userTask.get();
        int artCount=artCountTask.get();
        userinfoVO.setArtCount(artCount);

        HashMap<String,Object> result=new HashMap<>();
        result.put("userinfo",userinfoVO);
        result.put("articleinfo",articleinfo);

        return ResultAjax.succ(result);
    }

    @RequestMapping("/increment_rcount")
    public ResultAjax incrementRcount(Integer id){
        if(id==null||id<=0){
            return ResultAjax.fail(-1,"参数有误");
        }
        int result=articleService.incrementrcount(id);
        return ResultAjax.succ(result);
    }

    @RequestMapping("/getlists")
    public ResultAjax getlists(Integer pindex,Integer psize) throws ExecutionException, InterruptedException {
        if(pindex==null||pindex<1){
            pindex=1;
        }
        if(psize==null||psize<2){
            psize=2;
        }
        int offset=psize*(pindex-1);
        int psize1=psize;
        FutureTask<List<Articleinfo>> listTask=new FutureTask<>(()->{
           List<Articleinfo> list=articleService.getlists(psize1,offset);
           if(list!=null&&list.size()>0){
               list.stream().parallel().forEach((art)->{
                   if(art.getContent().length()>LENGTH){
                       art.setContent(art.getContent().substring(1,LENGTH));
                   }
               });
           }
           return list;
        });
        FutureTask<Integer> sizeTask=new FutureTask<>(()->{
            int totalcount=articleService.getcount();
            double sizeTemp=(totalcount*1.0)/(psize1*1.0);
            return (int)Math.ceil(sizeTemp);
        });
        taskExecutor.submit(listTask);
        taskExecutor.submit(sizeTask);
        int size=sizeTask.get();
        List<Articleinfo> list=listTask.get();
        HashMap<String,Object> map=new HashMap<>();
        map.put("list",list);
        map.put("size",size);

        return ResultAjax.succ(map);
    }
}
